package com.huluobo.a2111douyindemo.mine;


import android.content.Intent;
import android.util.Log;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.viewpager2.widget.ViewPager2;

import com.blankj.utilcode.util.SPUtils;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.huluobo.a2111douyindemo.R;
import com.huluobo.a2111douyindemo.adapter.CommentAdapter;
import com.huluobo.a2111douyindemo.adapter.CommonPagerAdapter;
import com.huluobo.a2111douyindemo.databinding.FragmentMineBinding;
import com.huluobo.a2111douyindemo.home.tabItem.CommonAdapter;
import com.huluobo.a2111douyindemo.home.tabItem.TabEntity;
import com.huluobo.a2111douyindemo.setting.SettingActivity;
import com.huluobo.a2111douyindemo.utils.ImageLoader;
import com.huluobo.mvvm_base.mvvm.base.view.BaseFragment;
import com.huluobo.mvvm_base.mvvm.base.view.BaseMVVMFragment;
import com.huluobo.mvvm_base.mvvm.bean.UserBeanResult;
import com.huluobo.mvvm_base.mvvm.net.Const;

import java.util.ArrayList;
import java.util.List;

public class MineFragment extends BaseMVVMFragment<FragmentMineBinding, MineViewModel> {

    //从数据库中查询到的用户信息
    private UserBeanResult.UserBean userInfo;
    //tabLayout的tab数据源
    private ArrayList<CustomTabEntity> tabEntities = new ArrayList<>();

    //Vp2的adapter
    private CommonPagerAdapter commonAdapter;
    //vp2的fragment数据源
    private List<Fragment> fragments = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initData() {
        viewModel.success.observe(this, userBean -> {
            Log.i("TagA","userBean:"+userBean.getIcon());
            userInfo = userBean;
            ImageLoader.getInstance().loadCircleImage(requireContext(), userInfo.getIcon(), dataBinding.mineUserIconIv);
            dataBinding.mineNicknameTv.setText(userInfo.getUsername());
        });

        viewModel.failed.observe(this, s -> {
            Log.i("TagA", "mineUserInfo:" + s);
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        //放到resume中,只要回到当前页面,就会重新读取一下用户信息
        viewModel.queryUserInfo(SPUtils.getInstance().getString(Const.SP_USERNAME));
    }

    @Override
    protected void initView() {

        tabEntities.clear();
        tabEntities.add(new TabEntity("作品", 0, 0));
        tabEntities.add(new TabEntity("秘密", 0, 0));
        tabEntities.add(new TabEntity("喜欢", 0, 0));
        tabEntities.add(new TabEntity("收藏", 0, 0));
        dataBinding.mineTabLayout.setTabData(tabEntities);

        fragments.clear();
        fragments.add(new WorksFragment());
        fragments.add(new WorksFragment());
        fragments.add(new WorksFragment());
        fragments.add(new WorksFragment());

        commonAdapter = new CommonPagerAdapter(requireActivity(), fragments);
        dataBinding.mineVp2.setAdapter(commonAdapter);

        dataBinding.mineTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                dataBinding.mineVp2.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        dataBinding.mineVp2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                dataBinding.mineTabLayout.setCurrentTab(position);
            }
        });

        dataBinding.mineSettingIv.setOnClickListener(v -> {
            Intent intent = new Intent(requireContext(), SettingActivity.class);
            startActivity(intent);
        });
    }

    @Override
    protected Class<MineViewModel> getViewModel() {
        return MineViewModel.class;
    }
}